package com.louis.kitty.admin.jasper.user.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.louis.kitty.admin.jasper.user.IUserReportJasper;
import com.louis.kitty.admin.model.SysUser;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;

/**
 * UserReportJasperImpl
 * qcd
 * 用户报表实现类
 */
public class UserReportJasperImpl implements IUserReportJasper{

	@Override
	public List<SysUser> getUserReport() {

		try {
			//加载DB驱动，并获得DB连接
		   Class.forName("oracle.jdbc.driver.OracleDriver");
		   String url = "jdbc:oracle:thin:@192.168.100.26:1521:LSSRTEST";
		   String user = "cam";
		   String password = "myPassword";
		   Connection conn = DriverManager.getConnection(url, user, password);
		  
		   //设定报表所需要的外部参数内容
		   Map<String, Object> rptParameters = new HashMap<String, Object>();
		   rptParameters.put("myParam", "hello world");
		  
		   //传入报表源文件绝对路径，外部参数对象，DB连接，得到JasperPring对象
		   JasperPrint jasperPrint = JasperFillManager.fillReport("D:/report1.jasper", rptParameters, conn);
		  
		   //导出PDF文件
		   JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/temp/jasper_test/test.pdf");
		  
		   //导入HTML文件
		   JasperExportManager.exportReportToHtmlFile(jasperPrint, "D:/temp/jasper_test/test.html");
		  
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (JRException e) {
			e.printStackTrace();
		}
		//执行结束
		return null;
	}
	
	public void mtest() throws FileNotFoundException {
		try {
			//加载DB驱动，并获得DB连接
		   Class.forName("com.mysql.jdbc.Driver");
		   String url = "jdbc:mysql://localhost:3306/kitty";
		   String user = "root";
		   String password = "root";
		   Connection conn = DriverManager.getConnection(url, user, password);
		  
		   //设定报表所需要的外部参数内容
		   Map<String, Object> rptParameters = new HashMap<String, Object>();
		   rptParameters.put("companyName", "南天	");
		  System.out.println("====加载参数====");
		   //传入报表源文件绝对路径，外部参数对象，DB连接，得到JasperPring对象
		   JasperPrint jasperPrint = JasperFillManager.fillReport("D:/report3.jasper", rptParameters, conn);
		   System.out.println("====得到JasperPring对象====");
		   //导出PDF文件
		   JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/test.pdf");
		  
		   //导入HTML文件
		   JasperExportManager.exportReportToHtmlFile(jasperPrint, "D:/test.html");
		   
		   FileOutputStream fos = new FileOutputStream(new File("D:/test.xls"));
		   SimpleXlsxReportConfiguration conf = new SimpleXlsxReportConfiguration();  
           conf.setWhitePageBackground(false);  
           conf.setDetectCellType(true);

           JRXlsxExporter exporter = new JRXlsxExporter();  
           exporter.setConfiguration(conf);  


           //设置输入项  
           ExporterInput exporterInput = new SimpleExporterInput(jasperPrint);  
           exporter.setExporterInput(exporterInput);  

           //设置输出项  
           OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(fos);  
           exporter.setExporterOutput(exporterOutput);  


           exporter.exportReport();
		   
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

}